package com.leinao.task;

import com.leinao.config.EnvironmentConfig;
import com.leinao.images.dao.ImagesMapper;
import com.leinao.images.dto.ImagesDto;
import com.leinao.util.SSHClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.concurrent.Callable;

public class ImageCallable implements Callable<Boolean> {
    private Logger logger = LoggerFactory.getLogger(ImageCallable.class);

    public ImageCallable(SSHClient sshClient, EnvironmentConfig envConfig, ImagesMapper imagesMapper, ImagesDto image) {
        this.sshClient = sshClient;
        this.envConfig = envConfig;
        this.imagesMapper = imagesMapper;
        this.image = image;
    }

    private SSHClient sshClient;
    private EnvironmentConfig envConfig;
    private ImagesMapper imagesMapper;
    private ImagesDto image;

    @Override
    public Boolean call() throws Exception {
        String dockerName = image.getName();
        Boolean result = false;
        logger.info("执行静态定时任务,镜像名:{}", dockerName);
        try {
            String dockerPath = envConfig.getDockerfilePath() + image.getUserId() + "/" + dockerName + "/Dockerfile";
            String buildCmd = "sudo docker build -f " + dockerPath + " -t " + envConfig.getImagesDomain() + envConfig.getImagesPath() + dockerName + "_" + image.getProvider() +" "+ envConfig.getDockerfilePath() + image.getUserId() + "/" + dockerName;
            logger.info("执行静态定时任务创建命令:{}", buildCmd);
            String ret = sshClient.sendCmd(buildCmd);
            if (ret != null && ret.contains("Successfully")) {
                logger.info("执行静态定时任务创建成功,镜像名:{}", dockerName);
                String imageCmd = "sudo docker images " + envConfig.getImagesDomain() + envConfig.getImagesPath() + dockerName + "_" + image.getProvider() ;
                ret = sshClient.sendCmd(imageCmd);
                String tt[] = ret.split(" ");
                String pushCmd = "sudo docker push " + envConfig.getImagesDomain() + envConfig.getImagesPath() + dockerName + "_" + image.getProvider() ;
                ret = sshClient.sendCmd(pushCmd);
                if (ret != null && ret.contains("digest")) {
                    logger.info("执行静态定时任务推送成功,镜像名:{}", dockerName);
                    ImagesDto imagesDto = new ImagesDto();
                    imagesDto.setId(image.getId());
                    imagesDto.setUserId(image.getUserId());
                    imagesDto.setStatus(2);
                    imagesDto.setSize(tt[tt.length - 1]);
                    Long time = System.currentTimeMillis();
                    imagesDto.setUpdateTime(time);
                    imagesMapper.updateUserImage(imagesDto);
                    result = true;
                } else {
                    logger.info("执行静态定时任务推送失败,镜像名:{}, 命令返回信息:{}", dockerName, ret);
                    ImagesDto imagesDto = new ImagesDto();
                    imagesDto.setId(image.getId());
                    imagesDto.setUserId(image.getUserId());
                    imagesDto.setStatus(3);
                    Long time = System.currentTimeMillis();
                    imagesDto.setUpdateTime(time);
                    imagesDto.setSize(tt[tt.length - 1]);
                    imagesMapper.updateUserImage(imagesDto);
                }
                String deleteCmd = "sudo docker rmi " + envConfig.getImagesDomain() + envConfig.getImagesPath() + dockerName + "_" + image.getProvider() ;
                ret = sshClient.sendCmd(deleteCmd);
                logger.info("执行静态定时任务删除镜像,镜像名:{}, 删除结果:{}", dockerName, ret);
            } else {
                logger.info("执行静态定时任务创建失败,镜像名:{}, 命令返回信息:{}", dockerName, ret);
                ImagesDto imagesDto = new ImagesDto();
                imagesDto.setId(image.getId());
                imagesDto.setUserId(image.getUserId());
                imagesDto.setStatus(3);
                Long time = System.currentTimeMillis();
                imagesDto.setUpdateTime(time);
                imagesMapper.updateUserImage(imagesDto);
            }
        } catch (Exception e) {
            logger.info("执行静态定时任务异常,镜像名:{}, 异常信息: {}", dockerName, e);
        }
        return result;
    }
}
